package com.ctu.gzero.operation
{
	import com.ctu.gzero.edit.ElementRange;
	import com.ctu.gzero.edit.SelectionState;
	import com.ctu.gzero.elements.TxtRun;
	import com.ctu.study.CharacterFormat;
	import com.ctu.study.ICharacterFormat;
	
	public class ApplyFormatOperation extends DomTextOperation
	{	
		private var _applyCharFormat:CharacterFormat;
//		private var _applyParaFormat:ParagraphFormat;
//		private var _applyContainerFormat:ContainerFormat;
		
		private var _undoCharArray:Array;
		private var _undoParaArray:Array;
		private var _undoContainerArray:Array;
		
		public function ApplyFormatOperation(selectionState:SelectionState, charFormat:ICharacterFormat/*, paraFormat:IPragraphFormat, containerFormat:IContainerFormat*/)
		{
			super(selectionState.dom);
			characterFormat = characterFormat;
//			paragraphFormat = paraFormat;
//			containerFormat = containerFormat 
		}
		
		public function get characterFormat():ICharacterFormat
		{
			return _applyCharFormat;
		}
		
		public function set characterFormat(characterFormat:ICharacterFormat):void
		{	
			_applyCharFormat = (characterFormat) ? CharacterFormat(characterFormat) : null; 	
		}
		
//		public function get paragraphFormat():IParagraphFormat
//		{
//			return _applyParaFormat;
//		}
//		
//		public function set paragraphFormat(paragraphFormat:IParagraphFormat):void
//		{
//			_applyParaFormat = (paragraphFormat) ? ParagraphFormat(paragraphFormat) : null;
//		}
//		
//		public function get containerFormat(containerFormat:IContainerFormat)
//		{
//			return _applyContainerFormat;
//		}
//		
//		public function set containerFormat(containerFormat:IContainerFormat):void
//		{
//			_applyContainerFormat = (containerFormat) ? ContainerFormat(containerFormat) : null;
//		}
	
		override public function doOperation():SelectionState
		{
			doInternal();			
			return originalSelectionState;
		}
		
		override public function undo():SelectionState
		{
			var _obj:Object = null;			
			var _holder:Array = _undoCharArray;
			
			for each(var _s in _holder)
			{
				_obj = _s;
				ParaEdit.setTextStyleChange(dom, _obj.begIndex, _obj.endIndex, _obj.style);
			}
			
			_holder = _undoParaArray;
			
			for each(var _s in _holder)
			{
				_obj = _s;
				ParaEdit.setParaStyleChange(dom, _obj.begIndex, _obj.endIndex, _obj.attributes);
			}
			
			_holder = _undoContainerArray;
			
			for each(var _s in _holder)
			{
				_obj = _s;
				ParaEdit.setContainerStyleChange(dom, _obj);
			}
			
			return originalSelectionState;
			
		}
		
		private function doInternal():void
		{
			var _elementRange:ElementRange = null;
			
			var _firstRun:TxtRun;
			var _lastRun:TxtRun;
			
			var _i:int = 0;
			var _j:int = 0;
			
			if(_applyCharFormat)
			{
				if(absoluteStart!=absoluteEnd)
				{
					_elementRange = ElementRange.createElementRange(this.dom, this.absoluteStart, this.absoluteEnd);
					_firstRun = _elementRange.firstRun;
					_lastRun = _elementRange.lastRun;
					
					if(!_undoCharArray)
					{
						_undoCharArray = Array();
						ParaEdit.cacheStyleInfo(dom, absoluteStart, absoluteEnd, _applyCharFormat);
					}
					ParaEdit.applyTextStyleChange(dom, absoluteStart, absoluteEnd, _applyCharFormat);
				}
			}
			
			if(_applyParaFormat)
			{
				if(!_undoParaArray)
				{
					_undoParaArray = new Array();
					ParaEdit.cacheParaStyleInfo(dom, absoluteStart, absoluteEnd, _undoParaArray);
				}	
				ParaEdit.applyParaStyleChange(dom, absoluteStart, absoluteEnd, _applyParaFormat);
			}
			
			if(_applyContainerFormat)
			{
				if(!_undoContainerArray)
				{
					_undoContainerArray = new Array();
					ParaEdit.cacheContainerStyleInfo(dom, absoluteStart, absoluteEnd, _undoContainerArray);
				}
				ParaEdit.applyContainerStyleChange(dom, absoluteStart, absoluteEnd, _applyContainerFormat);
			}
		}
		
		public function undo
	}
}